// packageD/directSeeding/components/answerPopup.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
    },
    infoData: {
      type: Object,
      value: {},
    },
    room_id: {
      type: null,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    answer: "",
    imgHost: app.globalData.imgPath + "live-broadcast-question/question.png",
    integral: "积分"
  },
  lifetimes: {
    attached() {
      this.setData({
        integral: wx.getStorageSync('integral')
      })
    }
  },
  observers: {
    show: function (showNew) {
      if (showNew) {
        this.setData({
          answer: "",
        });
      }
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    tapSelect(e) {
      let {
        id
      } = e.currentTarget.dataset;
      console.log(e, id, this.data.infoData);
      if (this.data.infoData.select_type == 1) {
        if (this.data.answer.length == 0) {
          this.data.answer = [];
          this.data.answer.push(id);
          this.setData({
            answer: this.data.answer,
          });
        } else {
          if (this.data.answer.indexOf(id) == -1) {
            this.data.answer.push(id);
            this.setData({
              answer: this.data.answer,
            });
          } else {
            this.data.answer.splice(this.data.answer.indexOf(id), 1);
            this.setData({
              answer: this.data.answer,
            });
          }
        }
      } else {
        // 单选
        this.setData({
          answer: id,
        });
      }
    },
    close() {
      this.triggerEvent("childClose", false);
    },
    async submitHandel() {
      let _json = {
        room_id: this.properties.room_id,
        question_id: this.data.infoData.id,
        answer_ids: this.data.infoData.select_type == 1 ?
          this.data.answer :
          [this.data.answer],
      };
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss(
          "plugin.live-broadcast-question.frontend.controllers.index.submit-answer"
        ),
        data: _json,
      });
      if (data.result) {
        this.triggerEvent("submit", data.data);
      }
    },
  },
});